/*
 * Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.greenrobot.greendao.sample;

import ohos.data.rdb.Statement;
import ohos.data.resultset.ResultSet;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.internal.DaoConfig;

/**
 * DAO for table "NOTE".
 */
public class PersonDao extends AbstractDao<Person, Long> {
    /**
     * TABLENAME
     */
    public static final String TABLENAME = "NOTE";

    /**
     * one
     */
    public static final int ONE = 1;

    /**
     * two
     */
    public static final int TWO = 2;

    /**
     * three
     */
    public static final int THREE = 3;

    /**
     * four
     */
    public static final int FOUR = 4;

    /**
     * five
     */
    public static final int FIVE = 5;

    /**
     * Six
     */
    public static final int SIX = 6;

    /**
     * Properties of entity Note.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     *
     * @since 2020-08-25
     */
    public static class Properties {
        /**
         * Id
         */
        public static final Property ID = new Property(0, Long.class, "id", true, "_id");

        /**
         * Text
         */
        public static final Property NAME = new Property(1, String.class, "name", false, "NAME");

        /**
         * Comment
         */
        public static final Property COMMENT = new Property(2, String.class, "comment", false, "COMMENT");

        /**
         * Comment
         */
        public static final Property PARENT = new Property(3, String.class, "parent", false, "PARENT");

        /**
         * Date
         */
        public static final Property DATE = new Property(4, java.util.Date.class, "date", false, "DATE");

        /**
         * Type
         */
        public static final Property TYPE = new Property(5, String.class, "type", false, "TYPE");
    }

    private final PersonTypeConverter typeConverter = new PersonTypeConverter();

    /**
     * NoteDao
     *
     * @param config DaoConfig
     */
    public PersonDao(DaoConfig config) {
        super(config);
    }

    /**
     * NoteDao
     *
     * @param config DaoConfig
     * @param daoSession DaoSession
     */
    public PersonDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     *
     * @param db database
     * @param isExists ifNotExists
     */
    public static void createTable(Database db, boolean isExists) {
        String constraint = isExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\"NOTE\" (" + //
            "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
            "\"NAME\" TEXT NOT NULL ," + // 1: name
            "\"COMMENT\" TEXT," + // 2: comment
            "\"PARENT\" TEXT," + // 3: parent
            "\"DATE\" INTEGER," + // 4: date
            "\"TYPE\" TEXT);"); // 5: type
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_NOTE_TEXT_DATE_DESC ON \"NOTE\""
            + " (\"TEXT\" ASC,\"DATE\" DESC);");
    }

    /**
     * Drops the underlying database table.
     *
     * @param db database
     * @param isExists isExists
     */
    public static void dropTable(Database db, boolean isExists) {
        String sql = "DROP TABLE " + (isExists ? "IF EXISTS " : "") + "\"NOTE\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Person entity) {
        stmt.clearBindings();
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(ONE, id);
        }
        stmt.bindString(TWO, entity.getName());

        String comment = entity.getComment();
        if (comment != null) {
            stmt.bindString(THREE, comment);
        }

        String parent = entity.getParent();
        if (parent != null) {
            stmt.bindString(FOUR, parent);
        }

        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(FIVE, date.getTime());
        }

        PersonType type = entity.getType();
        if (type != null) {
            stmt.bindString(SIX, typeConverter.convertToDatabaseValue(type));
        }
    }

    @Override
    protected final void bindValues(Statement stmt, Person entity) {
        stmt.clearValues();

        Long id = entity.getId();
        if (id != null) {
            stmt.setLong(ONE, id);
        }
        stmt.setString(TWO, entity.getName());

        String comment = entity.getComment();
        if (comment != null) {
            stmt.setString(THREE, comment);
        }

        String parent = entity.getParent();
        if (parent != null) {
            stmt.setString(FOUR, parent);
        }

        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.setLong(FIVE, date.getTime());
        }

        PersonType type = entity.getType();
        if (type != null) {
            stmt.setString(SIX, typeConverter.convertToDatabaseValue(type));
        }
    }

    @Override
    public Long readKey(ResultSet cursor, int offset) {
        return cursor.isColumnNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    public Person readEntity(ResultSet resultSet, int offset) {
        Person entity = new Person(
            resultSet.isColumnNull(offset + 0) ? null : resultSet.getLong(offset + 0), // id
            resultSet.getString(offset + ONE), // text
            resultSet.isColumnNull(offset + TWO) ? null : resultSet.getString(offset + TWO), // comment
            resultSet.isColumnNull(offset + THREE) ? null : resultSet.getString(offset + THREE), // comment
            resultSet.isColumnNull(offset + FOUR) ? null : new java.util.Date(resultSet.getLong(offset + FOUR)),
            // date
            resultSet.isColumnNull(offset + FIVE)
                ? null
                : typeConverter.convertToEntityProperty(resultSet.getString(offset + FIVE)) // type
        );
        return entity;
    }

    @Override
    public void readEntity(ResultSet resultSet, Person entity, int offset) {
        entity.setId(resultSet.isColumnNull(offset + 0) ? null : resultSet.getLong(offset + 0));
        entity.setName(resultSet.getString(offset + ONE));
        entity.setComment(resultSet.isColumnNull(offset + TWO) ? null : resultSet.getString(offset + TWO));
        entity.setParent(resultSet.isColumnNull(offset + THREE) ? null : resultSet.getString(offset + THREE));
        entity.setDate(
            resultSet.isColumnNull(offset + FOUR) ? null : new java.util.Date(resultSet.getLong(offset + FOUR)));
        entity.setType(resultSet.isColumnNull(offset + FIVE)
            ? null
            : typeConverter.convertToEntityProperty(resultSet.getString(offset + FIVE)));
    }

    @Override
    protected final Long updateKeyAfterInsert(Person entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    @Override
    public Long getKey(Person entity) {
        if (entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Person entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
}
